<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library("loadpage");
    }
    public function OnCreateMenu($strWhere){
          $this->load->model("mmenu");
          $mmenu = new MMenu();
          $menu1 = $mmenu->getOject($strWhere);
          return $menu1;
    }

    public function index() {
        /////////////////////////////
        ////load slide bar
        ////////////////////////////////

        $lp = new loadpage();
        $this->load->model("mkhuyenmai");
        $slide = new MKhuyenmai();
        $topid = $slide->get_top_id();
        $data = array();
        

        $strWhere = array("makm" => $topid);
        $strWhere1 = array("makm" => $topid - 1);
        $strWhere2 = array("makm" => $topid - 2);
        $strWhere3 = array("makm" => $topid - 3);

        $km = $slide->getOject($strWhere);
        $km1 = $slide->getOject($strWhere1);
        $km2 = $slide->getOject($strWhere2);
        $km3 = $slide->getOject($strWhere3);

        $data['makm'] = $km['makm'];
        $data['makm1'] = $km1['makm'];
        $data['makm2'] = $km2['makm'];
        $data['makm3'] = $km3['makm'];

        $data["tenkm"] = $km['tenkm'];
        $data["tenkm1"] = $km1['tenkm'];
        $data["tenkm2"] = $km2['tenkm'];
        $data["tenkm3"] = $km3['tenkm'];

        $data["noidung"] = $km['noidung'];
        $data["noidung1"] = $km1['noidung'];
        $data["noidung2"] = $km2['noidung'];
        $data["noidung3"] = $km3['noidung'];

        $data["hinhanh"] = $km['hinhanh'];
        $data["hinhanh1"] = $km1['hinhanh'];
        $data["hinhanh2"] = $km2['hinhanh'];
        $data["hinhanh3"] = $km3['hinhanh'];

        $data["tgbatdau"] = $km['tgbatdau'];
        $data["tgbatdau1"] = $km1['tgbatdau'];
        $data["tgbatdau2"] = $km2['tgbatdau'];
        $data["tgbatdau3"] = $km3['tgbatdau'];

        $data["tgketthuc"] = $km['tgketthuc'];
        $data["tgketthuc1"] = $km1['tgketthuc'];
        $data["tgketthuc2"] = $km2['tgketthuc'];
        $data["tgketthuc3"] = $km3['tgketthuc'];



        //////////////////////
        ///load menu
        /////////////////////////
        
          
        ///////////////////////////////////
        //load san pham noi bat
        //////////////////////////////////
        /*
         * panigation config 
         */
        $this->load->library('pagination');

        $config['base_url'] = base_url('index.php/welcome/index'); // xác định trang phân trang                 
        $data['title'] = "SẢN PHẨM NỔI BẬT";
        $this->load->model('mSanpham');

        $msp = new MSanpham();

        $config['total_rows'] = $msp->CountAll(); // xác định tổng số record 
        $config['per_page'] = 12; // xác định số record ở mỗi trang 
        $config['uri_segment'] = 3; // xác định segment chứa page number 
        $config['num_links'] = 2;
        //  $config['page_query_string'] = TRUE;
        //$config['query_string_segment']='a';
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['first_tag_open'] = '<li><a>';
        $config['first_tag_close'] = '</a></li>';
        $config['full_tag_open'] = '<li>';
        $config['full_tag_close'] = '</li>';
        $config['next_link'] = 'Next';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Prev';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Last';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = "<li class='current'><a>";
        $config['cur_tag_close'] = '</a></li>';
        $config['use_page_numbers'] = TRUE;
        $config['offset'] = '1';
        $this->pagination->initialize($config);
        $number = $config['per_page'];
        $offset = $this->uri->segment($config['uri_segment']);


        $data['sanpham'] = $msp->ListAll($number, $offset); //$msp->GetAllSanpham();
        $data['pagination'] = $this->pagination;
        
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        $lp->load(array('v_slidebar', 'v_product'), $data);
    }
    // get product by type
    public function show_productbytype() {
        $lp = new loadpage();
        $data = array();
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        //var_dump($_GET);
        if (isset($_GET['matl'])) {
            $matl = $_GET['matl'];
            $this->load->library('pagination');

            $config['base_url'] = base_url('index.php/welcome/show_productbytype?matl='.$matl); // xác định trang phân trang                 
            if ($matl == '1')
                $data['title'] = "LAPTOP";
            if ($matl == '2')
                $data['title'] = "ĐIỆN THOẠI";
            if ($matl == '3')
                $data['title'] = "MÁY TÍNH BẢNG";
            if ($matl == '4')
                $data['title'] = "ỐP LƯNG";
            if ($matl == '5')
                $data['title'] = "PIN DỰ PHÒNG";
            if ($matl == '6')
                $data['title'] = "THẺ NHỚ";
            if ($matl == '7')
                $data['title'] = "USB";
            if ($matl == '8')
                $data['title'] = "CHUỘT MÁY TÍNH";
            $this->load->model('msanpham');

            $msp = new MSanpham();

            $config['total_rows'] = $msp->CountAllByFill($matl); // xác định tổng số record 
            $config['per_page'] = 12; // xác định số record ở mỗi trang 
            $config['uri_segment'] = 3; // xác định segment chứa page number 
            $config['num_links'] = 2;

            $config['page_query_string'] = TRUE;
            $config['query_string_segment'] = 'page';
            $config['first_link'] = 'First';
            $config['last_link'] = 'Last';
            $config['first_tag_open'] = '<li><a>';
            $config['first_tag_close'] = '</a></li>';
            $config['full_tag_open'] = '<li>';
            $config['full_tag_close'] = '</li>';
            $config['next_link'] = 'Next';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_link'] = 'Prev';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['last_link'] = 'Last';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['cur_tag_open'] = "<li class='current'><a>";
            $config['cur_tag_close'] = '</a></li>';
            $config['use_page_numbers'] = TRUE;
            $config['offset'] = '1';
            $this->pagination->initialize($config);
            $number = $config['per_page'];
            $offset = $this->uri->segment($config['uri_segment']);
            if (isset($_GET['page']))
                $page = $_GET['page'];
            else
                $page = 1;
            $data['sanpham'] = $msp->GetProductByMatl($number, $page, $matl); //$msp->GetAllSanpham();
            $data['pagination'] = $this->pagination;
            $lp->load(array('v_product'), $data);
        }
    }
    public function show_productby2type() {
        $lp = new loadpage();
        $data = array();
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        //var_dump($_GET);
        if (isset($_GET['matl'])) {
            $matl = $_GET['matl'];
            $mahangsx = $_GET['hsx'];
            $this->load->library('pagination');

            $config['base_url'] = base_url('index.php/welcome/show_productby2type?matl='.$matl.'&hsx='.$mahangsx); // xác định trang phân trang                 
            if ($matl == '1')
                $data['title'] = "LAPTOP";
            if ($matl == '2')
                $data['title'] = "ĐIỆN THOẠI";
            if ($matl == '3')
                $data['title'] = "MÁY TÍNH BẢNG";
            $this->load->model('msanpham');

            $msp = new MSanpham();

            $config['total_rows'] = $msp->CountAllBy2Fill($matl,$mahangsx); // xác định tổng số record 
            $config['per_page'] = 12; // xác định số record ở mỗi trang 
            $config['uri_segment'] = 3; // xác định segment chứa page number 
            $config['num_links'] = 2;

            $config['page_query_string'] = TRUE;
            $config['query_string_segment'] = 'page';
            $config['first_link'] = 'First';
            $config['last_link'] = 'Last';
            $config['first_tag_open'] = '<li><a>';
            $config['first_tag_close'] = '</a></li>';
            $config['full_tag_open'] = '<li>';
            $config['full_tag_close'] = '</li>';
            $config['next_link'] = 'Next';
            $config['next_tag_open'] = '<li>';
            $config['next_tag_close'] = '</li>';
            $config['prev_link'] = 'Prev';
            $config['prev_tag_open'] = '<li>';
            $config['prev_tag_close'] = '</li>';
            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '</li>';
            $config['first_tag_open'] = '<li>';
            $config['first_tag_close'] = '</li>';
            $config['last_link'] = 'Last';
            $config['last_tag_open'] = '<li>';
            $config['last_tag_close'] = '</li>';
            $config['cur_tag_open'] = "<li class='current'><a>";
            $config['cur_tag_close'] = '</a></li>';
            $config['use_page_numbers'] = TRUE;
            $config['offset'] = '1';
            $this->pagination->initialize($config);
            $number = $config['per_page'];
            $offset = $this->uri->segment($config['uri_segment']);
            if (isset($_GET['page']))
                $page = $_GET['page'];
            else
                $page = 1;
            $data['sanpham'] = $msp->GetProductByMatlMahangsx($number, $page, $matl,$mahangsx); //$msp->GetAllSanpham();
            $data['pagination'] = $this->pagination;
            $lp->load(array('v_product'), $data);
        }
    }
    public function timkiem() {
        $tensp = $_POST['search'];
        $data = array();
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        $data['title'] = "KẾT QUẢ TÌM KIẾM : " . $tensp;
        $lp = new loadpage();

        $this->load->model("msanpham");
        $msp = new MSanpham();

        $data['sanpham'] = $msp->ListAllByName($tensp); //$msp->GetAllSanpham();
        $lp->load(array('v_searchresult'), $data);
    }

    public function show_contact() {
        
        $lp = new loadpage();
        $data = array();
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        $lp->load(array('v_contact'), $data);
    }

    public function vlogin() {
        $this->load->view('v_login');
    }

    public function vreg() {
        $this->load->view('v_regaccount');
    }

    public function vgetpass() {
        $this->load->view('v_getpassword');
    }

    public function vsearchadvance() {
        $lp = new loadpage();
        $this->load->model("mloaisanpham");
        $data = array();
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        $lsp = new MLoaisanpham();
        $data['loaisp'] = $lsp->getAllLoaisp();

        $this->load->model("mhangsanxuat");
        $hsx = new MHangsanxuat();
        $data['hangsx'] = $hsx->getAllhangsx();
        $lp->load(array('v_searchadvance'), $data);
    }

    public function advancesearch() {
        //gia ban
        $magia = $_POST['giaca'];
        if ($magia == "0") {
            $magia = null;
        }
        if ($magia == "1") {
            $magia = " giaban < 10000000";
        }
        if ($magia == "2") {
            $magia = " giaban > 10000000 and giaban < 15000000";
        }
        if ($magia == "3") {
            $magia = " giaban > 15000000 and giaban < 20000000";
        }
        if ($magia == "4") {
            $magia = " giaban > 20000000";
        }
        // sap xep, asc or desc

        $sapxep = $_POST['sapxep'];
        if ($sapxep == 1) {
            $sapxep = ' ORDER BY giaban asc';
        } else {
            $sapxep = ' ORDER BY giaban desc';
        }
        // hang san xuat
        if ($_POST['hangsx'] == "0") {
            $mahangsx = null;
        } else {
            $mahangsx = ' AND mahangsx ="' . $_POST['hangsx'] . '"';
        }
        //loai san pham
        if ($_POST['loaisp'] != "0") {
            $matl = ' AND matl="' . $_POST['loaisp'] . '"';
        } else {
            $matl = null;
        }

        // kiem tra where co ton tai hay k?
        $check = "";
        if ($magia != "" || $mahangsx != "" || $matl != "") {
            $check = "where";
        }
        if (!$magia) {

            $mahangsx = str_replace(" AND", "", $mahangsx);

            if (!$mahangsx) {
                $matl = str_replace(" AND", "", $matl);
            }
        }
        $magia = $check . $magia;
        $data = array();
        $data['title'] = "KẾT QUẢ TÌM KIẾM ";
        
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        $lp = new loadpage();

        $this->load->model("msanpham");
        $msp = new MSanpham();
        $data['sanpham'] = $msp->ListAllByQuery($magia, $mahangsx, $matl, $sapxep);
        $lp->load(array('v_searchresult'), $data);
    }

    public function lienhe() {
        $this->load->model("mContact");
        $ct = new Mcontact();
        $malh = "MLH" + $ct->get_top_id();
        $name = $_POST['name'];
        $email = $_POST['email'];
        $chude = $_POST['subject'];
        $message = $_POST['message'];
        //////////////////////
        $ct->set_malh($malh);
        $ct->set_email($email);
        $ct->set_tenlh($name);
        $ct->set_message($message);
        $ct->set_chude($chude);
        ////
        $ct->Save();
        redirect(base_url());
    }

    public function show_detailproduct($masp) {

        $lp = new loadpage();
        $this->load->model("msanpham");
        $msp = new MSanpham();
        $data = array();
        $data['menulaptop'] = $this->OnCreateMenu(1);
        $data['menudienthoai'] = $this->OnCreateMenu(2);
        $data['menumaytinhbang'] = $this->OnCreateMenu(3);
        
        $strWhere = array("masp" => $masp);
        $sp = $msp->getOject($strWhere);
        $data['masp'] = $sp['masp'];
        $data['tensp'] = $sp["tensp"];
        $data['matl'] = $sp["matl"];
        $data['machitiet'] = $sp["machitiet"];
        $data['giaban'] = $sp["giaban"];
        $data['hinhanh'] = $sp["hinhanh"];
        $data['mahangsx'] = $sp["mahangsx"];
        $data['is_noibat'] = $sp["is_noibat"];
        $data['makm'] = $sp["makm"];

        $this->load->model("mchitietsanpham");
        $mct = new MChitietsanpham();
        $strWhere2 = array("machitiet" => $sp["machitiet"]);
        $ctsp = $mct->getOject($strWhere2);
        $data['manhinh'] = $ctsp["manhinh"];
        $data['cpu'] = $ctsp["cpu"];
        $data['ram'] = $ctsp["ram"];
        $data['hedieuhanh'] = $ctsp["hedieuhanh"];
        $data['camerachinh'] = $ctsp["camerachinh"];
        $data['cameraphu'] = $ctsp["cameraphu"];
        $data['pin'] = $ctsp["pin"];
        $data['bonhotrong'] = $ctsp["bonhotrong"];
        $data['thenhongoai'] = $ctsp["thenhongoai"];
        $data['trongluong'] = $ctsp["trongluong"];

        $lp->load(array('v_detailproduct'), $data);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */